//
//  UIView+WTExtend.m
//  WTLiveSDKDemo
//
//  Created by ben on 16/10/13.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "UIView+WTExtend.h"
#import "KLCPopup.h"
#import <objc/runtime.h>

@implementation UIView (WTExtend)




- (UIViewController *) firstViewController {
    // convenience function for casting and to "mask" the recursive function
    return (UIViewController *)[self traverseResponderChainForUIViewController];
}

- (id) traverseResponderChainForUIViewController {
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    } else if ([nextResponder isKindOfClass:[UIView class]]) {
        return [nextResponder traverseResponderChainForUIViewController];
    } else {
        return nil;
    }
}

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(8.0, 8.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

- (void)showTip:(NSString*)title{
    
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    [window makeToast:title duration:2.0 position:CSToastPositionCenter];
}

- (void)showTip:(NSString *)title completion:(void(^)())completion{
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    [window makeToast:title duration:2.0 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
        completion();
    }];
}

#pragma mark setter & getter
- (void)setWt_popup:(KLCPopup *)wt_popup{
    objc_setAssociatedObject(self, @selector(wt_popup), wt_popup, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (KLCPopup *)wt_popup{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)popUpView:(KLCPopupHorizontalLayout)h_layout v_layout:(KLCPopupVerticalLayout)v_layout {
    
    KLCPopupLayout layout = KLCPopupLayoutMake(h_layout,v_layout);
    KLCPopup* popup = nil;
    if(!self.wt_popup){
        popup = [KLCPopup popupWithContentView:self
                                      showType:KLCPopupShowTypeGrowIn
                                   dismissType:KLCPopupDismissTypeGrowOut
                                      maskType:KLCPopupMaskTypeDimmed
                      dismissOnBackgroundTouch:YES
                         dismissOnContentTouch:NO];
        self.wt_popup = popup;
    }
    [self.wt_popup showWithLayout:layout];
}
@end
